package com.example.springdemo4;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/main1")
public class Main1 {
    @RequestMapping( "/m1")
    public String m1(){
        return "name:" ;
    }

    @RequestMapping("/m2")
    public String m2(String userName, Integer password) {
        return "userName:" + userName + "\npassword:" + password;
    }

    @RequestMapping("/m3")
    public String m3(User user) {
        return user.toString();
    }

    @RequestMapping("/m4")
    public String m4(String[] array) {
        return Arrays.toString(array);
    }

    @RequestMapping("/m5")
    public String m5(@RequestParam List<String> list) {
        return list.toString();
    }

    @RequestMapping("/m6")
    public String m6() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = new User();
        user.setName("李四");
        user.setAge(18);
        user.setId(1);
        //JAVA 对象转 JSON 字符串
        String json = objectMapper.writeValueAsString(user);
        System.out.println(json);

        //JSON 字符串转 JAVA 对象
        User user1 = objectMapper.readValue(json, User.class);
        System.out.println(user1.toString());
        return "转换成功";

    }

    @RequestMapping("/m7")
    public String m7(@RequestBody String name){
        return "name:" + name;
    }

    @RequestMapping("/m8/{name}/{age}")
    public String m8(@PathVariable String name, @PathVariable("age") Integer AGE){
        return "name:" + name + ",age" + AGE;
    }

    @RequestMapping("/m9")
    public String m9(@RequestPart("file")MultipartFile file) throws IOException {
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //文件上传到指定路径
        file.transferTo(new File("D:/gitee/" + fileName));
        return "接收到的文件名称为：" + fileName;
    }

    //获取Cookie
    @RequestMapping("/getCookie2")
    public String m11(@CookieValue("cookie") String Cookie) {
        return "成功获取到Cookie:" + Cookie;
    }

    //设置session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session != null) {

            session.setAttribute("userName", "张三");
        }
        return "session 设置成功";
    }
    //传统方式获取session
    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session != null) {
            String userName = (String)session.getAttribute("userName");
            return "userName:" + userName;
        }
        return "Session为空";
    }

    //简介方式(1)获取session
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        if(session != null) {
            String userName = (String)session.getAttribute("userName");
            return "userName:"+userName;
        }
        return "没有指定的session值";
    }
    //简介方式(2)获取session
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "userName", required = false) String userName) {
        if(userName == null) {
            return "Session为空";
        }
        return "userName:" + userName;
    }

    //获取header值
    @RequestMapping("/getHeader1")
    public String getHeader(HttpServletRequest request) {
        String header = request.getHeader("user-Agent");
        return "user-Agent:" + header;
    }

    @RequestMapping("/getHeader2")
    public String getHeader(@RequestHeader("user-Agent") String header) {
        return "user-Agent:" + header;
    }

}
